<template>
  <div>
    <div class="flex justify-between items-center">
      <div>子账号</div>
      <el-button type="primary" class="y" @click="handleAddPermissionUser()">新增</el-button>
    </div>
    <el-table :data="dkTableData" style="width: 100%; min-height: 300px" class="table">
      <el-table-column prop="uid" label="UID" width="80" align="center" />
      <el-table-column label="用户" min-width="80">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-image :src="scope.row.avatar" class="image" alt="">
              <template #error>
                <img src="@/assets/default/avatar.png" class="image" />
              </template>
            </el-image>
            <p class="over u-m-l-5">{{ scope.row.username }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button size="small" type="primary" @click="getPermissionForm(scope.row.id)">权限</el-button>
          <el-button size="small" type="danger" @click="deletePermission(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="app-container u-m-10 u-p-10">
      <el-pagination
        background
        layout="total,prev, pager, next"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        size="small"
        @current-change="currentChange"
      />
    </div>
    <dk-form ref="editForm" :form-data="formData" top="5vh" :is-dialog="true" @submit="getList()" />
  </div>
</template>

<script>
import { deletePermission, getAddFrom, getPermissionForm, getUserPermissionList } from '@/api/user'
import { getPlaid } from '@/utils/auth'

export default {
  name: 'PermissionList',
  props: {},
  data() {
    return {
      page: 1,
      pageSize: 10,
      total: 0,
      dkTableData: [],
      searchData: {},
      order: '',
      dialogFormVisible: false,
      formData: null
    }
  },
  computed: {},
  mounted() {
    this.getList(1)
  },
  methods: {
    handleAddPermissionUser() {
      getAddFrom(getPlaid()).then((res) => {
        this.formData = res.data
        // this.$refs.editForm.dialogFormVisible = true
        this.$refs.editForm.show()
      })
    },
    getPermissionForm(id) {
      getPermissionForm(id, getPlaid()).then((res) => {
        this.formData = res.data
        this.$refs.editForm.show()
      })
    },
    currentChange(page) {
      this.getList(page)
    },
    getList(page) {
      this.page = page
      getUserPermissionList(this.page, this.pageSize, this.searchData, getPlaid()).then((res) => {
        this.dkTableData = res.data
        this.total = parseInt(res.msg)
      })
    },
    deletePermission(id) {
      // 确定要删除
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deletePermission(id).then((res) => {
            this.$message.success(res.msg)
            this.getList(this.page)
          })
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped lang="scss">
.image {
  width: 50px;
  height: 50px;
  border-radius: 100%;
  margin-right: 10px;
}
</style>
